In [1]:
pwd
Out[1]:
'/home/dchen/swc/python'
In [3]:
# this is a comment and will not be run
print('hello everyone')
hello everyone

In [4]:
'''
this
is a 
multi line comment
'''

"""
so
is this
"""
print('hello again')
hello again

In [5]:
type(3)
Out[5]:
int
In [6]:
type(3.4)
Out[6]:
float
In [8]:
type(False)
Out[8]:
bool
In [12]:
type("3")
Out[12]:
str
In [15]:
x = True
y = 3
z = "cat"
aa = "45"
print(x, y, z, aa)
True 3 cat 45

In [16]:
3 + 3
Out[16]:
6
In [18]:
3/2.0
Out[18]:
1.5
In [19]:
# exponents
2 ** 3
Out[19]:
8
In [20]:
# modulo
10 % 3
Out[20]:
1
In [22]:
3 > 1
Out[22]:
True
In [24]:
# string concatenation
"cat" + " and dog"
Out[24]:
'cat and dog'
In [26]:
# using only the number 4 and 6
# 8

6 + 6 - 4
Out[26]:
8
In [27]:
# this is but a flesh wound
'this is but' + ' a flesh wound'
Out[27]:
'this is but a flesh wound'
In [28]:
# lists
my_list = []
In [29]:
type(my_list)
Out[29]:
list
In [30]:
[1, 3, 4, 5, 6, 10]
Out[30]:
[1, 3, 4, 5, 6, 10]
In [31]:
[1, 3, "3", True]
Out[31]:
[1, 3, '3', True]
In [33]:
my_list = [1, 3, 5, True]
print(my_list)
[1, 3, 5, True]

In [34]:
atgc = ['adenine', 'guaniene', 'thyeme', 'cytoscene?']
In [36]:
atgc[0]
Out[36]:
'adenine'
In [39]:
atgc[-3]
Out[39]:
'guaniene'
In [43]:
# slicing
atgc[1:3]
Out[43]:
['guaniene', 'thyeme']
In [45]:
atgc[:3]
Out[45]:
['adenine', 'guaniene', 'thyeme']
In [47]:
atgc.append('uracil')
In [48]:
atgc
Out[48]:
['adenine', 'guaniene', 'thyeme', 'cytoscene?', 'uracil', 'uracil']
In [49]:
id(atgc[4])
Out[49]:
139921111316720
In [50]:
id(atgc[5])
Out[50]:
139921111316720
In [51]:
atgc.pop()
Out[51]:
'uracil'
In [53]:
del atgc[2]
In [54]:
atgc
Out[54]:
['adenine', 'guaniene', 'cytoscene?', 'uracil']
In [55]:
type(atgc)
Out[55]:
list
In [60]:
atgc[3] = 'tyieme'
atgc
Out[60]:
['adenine', 'guaniene', 'cytoscene?', 'tyieme']
In [57]:
my_tuple = ()
type(my_tuple)
Out[57]:
tuple
In [62]:
my_tuple = (3, 4, 5)
my_tuple[1] = 99
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-b657fa7e9169> in <module>()
      1 my_tuple = (3, 4, 5)
----> 2 my_tuple[1] = 99

TypeError: 'tuple' object does not support item assignment
In [66]:
x = [5, 6, 7]
y = x
print(x, y)
[5, 6, 7] [5, 6, 7]

In [68]:
x[0] = 23
print(x)
[23, 6, 7]

In [69]:
print(y)
[23, 6, 7]

In [70]:
id(x)
Out[70]:
139921111254216
In [71]:
id(y)
Out[71]:
139921111254216
In [72]:
help(list)
Help on class list in module builtins:

class list(object)
 |  list() -> new empty list
 |  list(iterable) -> new list initialized from iterable's items
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iadd__(self, value, /)
 |      Implement self+=value.
 |  
 |  __imul__(self, value, /)
 |      Implement self*=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __mul__(self, value, /)
 |      Return self*value.n
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __reversed__(...)
 |      L.__reversed__() -- return a reverse iterator over the list
 |  
 |  __rmul__(self, value, /)
 |      Return self*value.
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __sizeof__(...)
 |      L.__sizeof__() -- size of L in memory, in bytes
 |  
 |  append(...)
 |      L.append(object) -> None -- append object to end
 |  
 |  clear(...)
 |      L.clear() -> None -- remove all items from L
 |  
 |  copy(...)
 |      L.copy() -> list -- a shallow copy of L
 |  
 |  count(...)
 |      L.count(value) -> integer -- return number of occurrences of value
 |  
 |  extend(...)
 |      L.extend(iterable) -> None -- extend list by appending elements from the iterable
 |  
 |  index(...)
 |      L.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.
 |  
 |  insert(...)
 |      L.insert(index, object) -- insert object before index
 |  
 |  pop(...)
 |      L.pop([index]) -> item -- remove and return item at index (default last).
 |      Raises IndexError if list is empty or index is out of range.
 |  
 |  remove(...)
 |      L.remove(value) -> None -- remove first occurrence of value.
 |      Raises ValueError if the value is not present.
 |  
 |  reverse(...)
 |      L.reverse() -- reverse *IN PLACE*
 |  
 |  sort(...)
 |      L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None


In [78]:
my_list.sort()
print(my_list)
[1, True, 3, 5]

In [89]:
names = ["daniel", "gabe", "camille"]
In [88]:
print(names.sort())
None

In [81]:
names
Out[81]:
['camille', 'daniel', 'gabe']
In [87]:
my_string = "thisisastring"
print(my_string)

#capitalize first word
print(my_string.capitalize())

# did this change my variable?
print(my_string)
      
my_string = my_string.capitalize()
print(my_string)
thisisastring
Thisisastring
thisisastring
Thisisastring

In [91]:
# is 'daniel' in my list of names?
type("daniel" in names)
Out[91]:
bool
In [94]:
len("daniel")
Out[94]:
6
In [95]:
# dictionaries
my_dic = {}
In [96]:
type(my_dic)
Out[96]:
dict
In [98]:
# creating a dictionary key:value
my_dic = {"daniel":"chen", "camille":"ostrichnSpanish"}
In [99]:
print(my_dic)
{'daniel': 'chen', 'camille': 'ostrichnSpanish'}

In [100]:
# getting value out of dictionary
my_dic["daniel"]
Out[100]:
'chen'
In [101]:
# re-assigning a value of a key in a dictionary
my_dic["daniel"] = "wild goose"
In [102]:
my_dic
Out[102]:
{'daniel': 'wild goose', 'camille': 'ostrichnSpanish'}
In [103]:
# adding new key:value pair to list
my_dic["gabe"] = "periz-something"
In [104]:
my_dic
Out[104]:
{'gabe': 'periz-something',
 'daniel': 'wild goose',
 'camille': 'ostrichnSpanish'}
In [105]:
# deleting element from list
del my_dic['daniel']
In [106]:
my_dic
Out[106]:
{'gabe': 'periz-something', 'camille': 'ostrichnSpanish'}
In [107]:
my_dic['daniel'] = 25
In [108]:
my_dic
Out[108]:
{'gabe': 'periz-something', 'daniel': 25, 'camille': 'ostrichnSpanish'}
In [109]:
# return value
my_dic['daniel']
Out[109]:
25
In [110]:
my_dic['asdf']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-110-bd0a071458f6> in <module>()
----> 1 my_dic['asdf']

KeyError: 'asdf'
In [111]:
my_dic.get('daniel')
Out[111]:
25
In [113]:
type(my_dic.get('asdf'))
Out[113]:
NoneType
In [114]:
'daniel' in my_dic
Out[114]:
True
In [115]:
'asdf' in my_dic
Out[115]:
False

question

 - how would you assign these values to a python dictionary?

key | value

fname | daniel

lname | chen

age | 25

height | 69

weight | 180

- how would you store the above data in a single dictionary entry?

(You could accomplish this using a list of the above values, but a more straightforward solution is like a database: simply store the dictionary above with an appropriate key, such as a number or name.)

In [116]:
# oops
my_dic = {fname:"daniel", lname:"chen", age:25, height:69, weight:180}
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-116-36199c6bf70f> in <module>()
----> 1 my_dic = {fname:"danie", lname:"chen", age:25, height:69, weight:180}

NameError: name 'fname' is not defined
In [117]:
my_dic = {"fname":"daniel", "lname":"chen", "age":25, "height":69, "weight":180}
In [118]:
my_dic
Out[118]:
{'height': 69, 'age': 25, 'lname': 'chen', 'weight': 180, 'fname': 'daniel'}
In [119]:
fname = 'daniel_again'
lname = 'wild goose'
age = 99
height = 34
weight = 444
In [120]:
my_dic = {"fname":fname, "lname":lname, "age":age, "height":height, "weight":weight}
In [121]:
my_dic
Out[121]:
{'height': 34,
 'age': 99,
 'lname': 'wild goose',
 'weight': 444,
 'fname': 'daniel_again'}
In [122]:
my_other_dic = {1: my_dic}
In [123]:
my_other_dic
Out[123]:
{1: {'height': 34,
  'age': 99,
  'lname': 'wild goose',
  'weight': 444,
  'fname': 'daniel_again'}}
In [125]:
my_other_dic[1]
Out[125]:
{'height': 34,
 'age': 99,
 'lname': 'wild goose',
 'weight': 444,
 'fname': 'daniel_again'}
In [126]:
my_other_dic.get(1)
Out[126]:
{'height': 34,
 'age': 99,
 'lname': 'wild goose',
 'weight': 444,
 'fname': 'daniel_again'}
In [130]:
#getting value from a dict
# where this dic is a value of a parent dict
my_other_dic.get(1).get('fname')
Out[130]:
'daniel_again'
In [129]:
# merging dicts
my_new_dict = {}
In [137]:
my_new_dict.update(my_other_dic)
In [138]:
my_new_dict
Out[138]:
{1: {'height': 34,
  'age': 99,
  'lname': 'wild goose',
  'weight': 444,
  'fname': 'daniel_again'},
 'age': 99,
 'height': 34,
 'fname': 'daniel_again',
 'lname': 'wild goose',
 'weight': 444}
In [139]:
# flow control
In [140]:
# if/else

if (something boolean): something to do if true someting else to do if ture and yet another

this will be run regarless

In [148]:
if "asdf" == "daniel":
    print('hello i can spell!')
    print(x_1)
    x_1 = x_1 **2
    print(x_1)

print('boo')
boo

In [153]:
names = ["daniel", "chicken"]

if "asdf" in names:
    print('daniel is in names')
elif 'chicken' in names:
    print('found a chicken')
else:
    print("i found nothing")
    
print('finished')
found a chicken
finished

In [157]:
10 % 3
Out[157]:
1
In [177]:
# given a dict of fname, lname, and age
my_dict = {'fname':'daniel', 'lname':'chn', 'age':25}

# if your fname has the letter 'e', print your first name
# if your lname does not have the letter 'e', print your last name
# otherwise, print your age
In [178]:
# if your fname has the letter 'e', print your first name
if 'e' in my_dict['fname']:
    print(my_dict.get('fname'))

# if your lname does not have the letter 'e', print your last name
elif (not ('e' in my_dict.get('lname'))):
    print(my_dict['lname'])

# otherwise, print your age
else:
    print(my_dict['age'])
daniel

In [161]:
'daniel' == 'daniel'
Out[161]:
True
In [160]:
not 'daniel' == 'daniel'
Out[160]:
False
In [163]:
my_list = [1, 2, 3, 4, 5]
In [164]:
4 in my_list
Out[164]:
True
In [166]:
'd' in 'daniel'
Out[166]:
True
In [167]:
my_dict
Out[167]:
{'age': 25, 'lname': 'chen', 'fname': 'daniel'}
In []: